home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: Are Pure Functions always Virtual????
- Date: 5 Jan 1996 20:51:00 +1100
- Organization: Werple Internet, Melbourne
- Message-ID: <4cisa4$g1n@werple.net.au>
- References: <4cilse$5li@news2.deltanet.com>
- NNTP-Posting-Host: werple.mira.net.au
-
- olivas@deltanet.com (Sergio Olivas) writes:
-
-
- >I have a base class for accessing databases (BC++), the base class is
- >made of only pure virtual functions. Two derived classes are written,
- >one for accessing databases through the Paradox Engine, the other
- >using the Borland Database Engine.
-
- >Since I've heard that using virtual functions really eats into the 64k
- >automatic data segment, as well as adding overhead, is the 'virtual'
- >keyword really needed. -- and does it make any difference, assuming
- >I'm not going to further derive another class from the newly derived
- >class (in this case DB_BASE_PDX) ???
-
- >eg..
- > class DB_BASE {
- >{ ...
- > virtual int NextRecord(int TableID) = 0;
- > ..^^^^^ is this needed?
- > };
-
- >The derived classes are something like
- > class DB_BASE_PDX : public DB_BASE
- > {
- > ...
- > int NextRecord(int TableID);
- > ...
- > }
-
- The reason for having virtual functions, pure or not, is that you intend
- to call the functions through a pointer or reference to the base class.
- For example:
- void f(DB_BASE *p)
- { p->NextRecord(1);
- }
- This code will call the function defined in DB_BASE_PDX, or in whatever is
- the true class of object that 'p' points to. If you want this capability,
- then you need the pure virtual functions. However, if you only ever deal
- with the actual object, or a pointer or reference to the object's actual
- class, you don't need any virtual functions; you can just define
- NextRecord in each of the derived classes as ordinary functions and don't
- mention them at all in the base class.
-
- The amount of memory consumed by the v-tables depends on how many virtual
- functions there are and on how many classes are in the hierarchy. In a
- typical implementation, in each non-abstract class (i.e., each class for
- which no pure virtual functions, in itself or inherited, remain to be
- implemented by real functions) one pointer is required for each virtual
- function in itself and its base classes. An abstract class, it seems to
- me, would not require a v-table.
-
- As far as the overhead is concerned (I presume you mean execution time
- overhead), unless the code in your virtual functions is trivial, you are
- unlikely to notice it.
-
- Lastly, a pure function must be virtual; otherwise, it has no meaning and
- no purpose.
-
- David White
- davidw@werple.mira.net.au
-